Github
Poststypescripttypescript utility

typescript utility

TypeScript 유틸리티 타입들(Partial, Readonly, Record, Pick, Omit 등)의 사용법과 예제
  1. Partial<T>

    T의 모든 프로퍼티를 옵셔널로 만든다. 주어진 타입의 모든 하위 타입 집합을 나타내는 타입을 반환한다.

  2. Readonly<T>

    T의 모든 프로퍼티를 읽기 전영으로 설정한 타입을 반환한다.

    Object.freeze를 만들 수 있다.

  3. Record<K ,T> T의 프로퍼티의 집합 K로 타입을 구성한다. 타입의 프로퍼티들을 다른 타입에 매핑시키는 데 사용한다.

1 interface PageInfo { 2 title: string; 3 } 4 5 type Page = 'home' | 'about' | 'contact'; 6 7 const x: Record<Page, PageInfo> = { 8 about: { title: 'about' }, 9 contact: { title: 'contact' }, 10 home: { title: 'home' }, 11 };

K의 집합을 key로 구성하고, 각 key는 PageInfo로 type을 매핑한다.

  1. Pick<T, K>

    T에서 프로퍼티 K의 집합을 선택해 타입을 구성한다.

    1interface Todo { 2 title: string; 3 description: string; 4 completed: boolean; 5} 6 7type TodoPreview = Pick<Todo, 'title' | 'completed'>; 8 9const todo: TodoPreview = { 10 title: 'Clean room', 11 completed: false, 12};

    TodoPreview는 K의 집합을 선택적으로 key로 가질 수 있다.

  2. Omit<T, K>

    T에서 모든 프로터티를 선택한 다음 K를 제거한 타입을 구성한다.

    1interface Todo { 2 title: string; 3 description: string; 4 completed: boolean; 5} 6 7type TodoPreview = Omit<Todo, 'description'>; 8 9const todo: TodoPreview = { 10 title: 'Clean room', 11 completed: false, 12};
  3. Exclude<T, U>

    T에서 U와 겹치는 프로퍼티를 제외한 타입을 구성한다.

    1type T0 = Exclude<"a" | "b" | "c", "a">; // "b" | "c" 2type T1 = Exclude<"a" | "b" | "c", "a" | "b">; // "c" 3type T2 = Exclude<string | number | (() => void), Function>; // string | number
  4. Extract<T, U>

    T에서 U와 겹치는 프로퍼티로 타입을 구성한다.

    1type T0 = Extract<"a" | "b" | "c", "a" | "f">; // "a" 2type T1 = Extract<string | number | (() => void), Function>; // () => void
  5. NonNullable<T>

    T에서 null과 undefined를 제외한 타입을 구성한다.

  6. Parameters<T>

    함수 타입 T의 매개변수 타입들의 튜플 타입을 구성한다.

    1declare function f1(arg: { a: number, b: string }): void 2 3type T4 = Parameters<typeof f1>; // [{ a: number, b: string }] 4 5--- 6 7type T1 = Parameters<(s: string) => void>; // [string]
  7. ConstuctorParameters<T>

    생성자 함수 타입의 매개변수 타입의 튜플 타입을 구성한다.

    1type T0 = ConstructorParameters<ErrorConstructor>; // [(string | undefined)?] 2type T1 = ConstructorParameters<FunctionConstructor>; // string[] 3type T2 = ConstructorParameters<RegExpConstructor>; // [string, (string | undefined)?]
  8. ReturnType<T>

    함수 타입 T의 반환 타입으로 구성된다.

    1declare function f1(): { a: number, b: string } 2 3type T4 = ReturnType<typeof f1>; // { a: number, b: string } 4 5--- 6 7type T1 = ReturnType<(s: string) => void>; // void
  9. InstanceType<T>

    생성자 함수 타입 T의 인스턴스 타입으로 구성된 타입을 만든다.

    1class C { 2 x = 0; 3 y = 0; 4} 5 6type T0 = InstanceType<typeof C>; // C
  10. Required<T>

    T의 모든 프로퍼티가 필수로 설정된 타입을 구성한다.

    1interface Props { 2 a?: number; 3 b?: string; 4}; 5 6const obj2: Required<Props> = { a: 5 }; // 오류: 프로퍼티 'b'가 없습니다
  11. ThisParameterType

    함수 타입에 this 매개변수의 타입을 추출한다.

  12. OmitThisParameter

    함수 타입에 this 매개변수를 제거한다.

  13. ThisType<T>

1type ObjectDescriptor<D, M> = { 2 data?: D; 3 methods?: M & ThisType<D & M>; // 메서드 안의 'this 타입은 D & M 입니다. 4} 5 6function makeObject<D, M>(desc: ObjectDescriptor<D, M>): D & M { 7 let data: object = desc.data || {}; 8 let methods: object = desc.methods || {}; 9 return { ...data, ...methods } as D & M; 10} 11 12let obj = makeObject({ 13 data: { x: 0, y: 0 }, 14 methods: { 15 moveBy(dx: number, dy: number) { 16 this.x += dx; // 강하게 타입이 정해진 this 17 this.y += dy; // 강하게 타입이 정해진 this 18 } 19 } 20}); 21 22obj.x = 10; 23obj.y = 20; 24obj.moveBy(5, 5);